<?php
class XA_Data_PgSQL extends XA_Data {

	public function __construct($result_resource) {
		$this->result_resource = $result_resource;
	}


	// DEPRECIATED, exists for backward compat., will be removed
	public function getRow() {
		return $this->row();
	}


	public function row() {
		return pg_fetch_assoc($this->result_resource);
	}


	public function getRows($index = null) {
		return $this->all($index);
	}


	public function all($index = null) {
		$return = array();
		if (pg_num_rows($this->result_resource)) {
			pg_result_seek($this->result_resource, 0);
			while ($row = pg_fetch_assoc($this->result_resource)) {
				if ($index) {
					$return[$row[$index]] = $row;
				} else {
					$return[] = $row;
				}
			}
		}
		return $return;
	}


	// DEPRECIATED, will be removed
	public function getOptionList($key_name, $value_name) {
		return $this->column($value_name, $key_name);
	}


	public function reset() {
		pg_result_seek($this->result_resource, 0);
	}


	// DEPRECIATED
	public function getRowCount() {
		return pg_num_rows($this->result_resource);
	}


	public function count() {
		return pg_num_rows($this->result_resource);
	}


	public function getValue($key) {
		return $this->val($key);
	}


	public function val($key = null) {
		if ($key) {
			return pg_fetch_result($this->result_resource, $key);
		} else {
			return current(pg_fetch_row($this->result_resource));
		}
	}


	public function getValues($key, $index = null) {
		return $this->column($key, $index);
	}


	// TO BE OPTIMISED with pg_fetch_all_columns() or pg_fetch_result()...
	public function column($key, $index = null) {
		$return = array();
		while ($row = pg_fetch_assoc($this->result_resource)) {
			if ($index) {
				$return[$row[$index]] = $row[$key];
			} else {
				$return[] = $row[$key];
			}
		}
		return $return;
	}
}
